package com.ruoyi.patients.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.patients.domain.Conflict;
import com.ruoyi.patients.domain.Patients;
import com.ruoyi.patients.mapper.PatientsMapper;
import com.ruoyi.patients.service.IPatientsService;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.system.service.ISysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 患者Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-04-12
 */
@Service
public class PatientsServiceImpl implements IPatientsService {
    @Autowired
    private PatientsMapper patientsMapper;

    @Autowired
    private ISysUserService userService;

    @Autowired
    private ISysRoleService roleService;

    /**
     * 查询患者
     * 
     * @param id 患者主键
     * @return 患者
     */
    @Override
    public Patients selectPatientsById(Long id) {
        return patientsMapper.selectPatientsById(id);
    }

    /**
     * 查询患者列表
     * 
     * @param patients 患者
     * @return 患者
     */
    @Override
    public List<Patients> selectPatientsList(Patients patients) {
        List<Patients> patientsList = patientsMapper.selectPatientsList(patients);
        for (Patients p : patientsList) {
            List<Conflict> conflictList = patientsMapper.selectConflictList(p.getId());
            p.setConflictList(conflictList);
        }
        return patientsList;
    }

    /**
     * 新增患者
     * 
     * @param patients 患者
     * @return 结果
     */
    @Transactional
    @Override
    public int insertPatients(Patients patients) {
        patients.setCreateTime(DateUtils.getNowDate());
        patients.setUpdateTime(DateUtils.getNowDate());
        int rows = patientsMapper.insertPatients(patients);
        insertConflict(patients);

        // 创建对应的用户账号
        if (StringUtils.isNotEmpty(patients.getUser())) {
            SysUser sysUser = new SysUser();
            sysUser.setUserName(patients.getUser());
            sysUser.setNickName(patients.getName());
            sysUser.setPassword("123456"); // 默认密码
            sysUser.setStatus("0"); // 正常状态
            sysUser.setDelFlag("0"); // 未删除
            userService.insertUser(sysUser);

            // 获取患者角色
            SysRole role = new SysRole();
            role.setRoleKey("patient");
            List<SysRole> roles = roleService.selectRoleList(role);
            if (!roles.isEmpty()) {
                // 分配患者角色
                userService.insertUserAuth(sysUser.getUserId(), new Long[] { roles.get(0).getRoleId() });
            }
        }

        return rows;
    }

    /**
     * 修改患者
     * 
     * @param patients 患者
     * @return 结果
     */
    @Transactional
    @Override
    public int updatePatients(Patients patients) {
        patientsMapper.deleteConflictByPatientsId(patients.getId());
        insertConflict(patients);
        return patientsMapper.updatePatients(patients);
    }

    /**
     * 批量删除患者
     * 
     * @param ids 需要删除的患者主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deletePatientsByIds(Long[] ids) {
        // 获取所有要删除的患者信息
        for (Long id : ids) {
            Patients patients = patientsMapper.selectPatientsById(id);
            if (patients != null && StringUtils.isNotEmpty(patients.getUser())) {
                // 删除对应的用户账号
                SysUser sysUser = userService.selectUserByUserName(patients.getUser());
                if (sysUser != null) {
                    userService.deleteUserById(sysUser.getUserId());
                }
            }
        }

        patientsMapper.deleteConflictByPatientsIds(ids);
        return patientsMapper.deletePatientsByIds(ids);
    }

    /**
     * 删除患者信息
     * 
     * @param id 患者主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deletePatientsById(Long id) {
        // 获取患者信息
        Patients patients = patientsMapper.selectPatientsById(id);
        if (patients != null && StringUtils.isNotEmpty(patients.getUser())) {
            // 删除对应的用户账号
            SysUser sysUser = userService.selectUserByUserName(patients.getUser());
            if (sysUser != null) {
                userService.deleteUserById(sysUser.getUserId());
            }
        }

        patientsMapper.deleteConflictByPatientsId(id);
        return patientsMapper.deletePatientsById(id);
    }

    /**
     * 新增药品冲突信息
     * 
     * @param patients 患者对象
     */
    public void insertConflict(Patients patients) {
        List<Conflict> conflictList = patients.getConflictList();
        Long id = patients.getId();
        if (StringUtils.isNotNull(conflictList)) {
            List<Conflict> list = new ArrayList<Conflict>();
            for (Conflict conflict : conflictList) {
                conflict.setPatientsId(id);
                conflict.setCreateTime(DateUtils.getNowDate());
                list.add(conflict);
            }
            if (list.size() > 0) {
                patientsMapper.batchConflict(list);
            }
        }
    }
}
